package velib.view;

import java.awt.event.ActionListener;
import java.util.Vector;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

public class MainWindowFrame extends javax.swing.JFrame implements IView
{

    // Register middleContentPanels ref for previous button
    private Vector<JPanel> middleContentPanelsVector;

    /** Creates new form MainWindow */
    public MainWindowFrame()
    {
        initComponents();
        initMainWindow();
        middleContentPanelsVector = new Vector<JPanel>();
    }

    /*
     * Set up the first screen which is the WelcomeScreen
     */
    public final void initMainWindow()
    {
        middleContentPanel.removeAll();
        middleContentPanelsVector = new Vector<JPanel>();
    }

    public void showMessage(String message)
    {
        JOptionPane.showMessageDialog(this, message);
    }


    public void creationDBPopup()
    {
        JOptionPane.showMessageDialog(this, "Aucune base de donnée n'a été trouvée. Une a été crée.");
    }


    /*
     * Replaces the current content panel by the given one
     */
    public void setContentPanel(JPanel panel)
    {
        // saving the panel reference (used for later usage)
        middleContentPanelsVector.add(panel);

        // remove all the previous content to add the new panel
        middleContentPanel.removeAll();
        middleContentPanel.add(panel);
        panel.setVisible(true);

        // redraw the middle content
        middleContentPanel.revalidate();
        middleContentPanel.repaint();

        // set previous/next buttons states
        // TODO: we can auto update previous/next status by creating
        // a widget that listen on middleContentPanelsVector events
        if (middleContentPanelsVector.size() > 1)
        {
            enablePreviousButton(true);
        }
    }

    private void enablePreviousButton(boolean enable)
    {
        previousButton.setEnabled(enable);
    }

    private void enableNextButton(boolean enable)
    {
        nextButton.setEnabled(enable);
    }

    public void addNextButtonListener(ActionListener al)
    {
        nextButton.addActionListener(al);
    }

    private void previousPanel()
    {
        JPanel previousPanel, currentPanel;

        /*
         * Remove the element so the order won't get broken
         * when adding new panels
         */
        currentPanel =
                middleContentPanelsVector.lastElement();
        middleContentPanelsVector.remove(currentPanel);
        
        previousPanel =
                middleContentPanelsVector.lastElement();

        setContentPanel(previousPanel);
    }


    public void addDBConnectionButtonListener(ActionListener al)
    {
        dbConnectionMenuItem.addActionListener(al);
    }

    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        middleContentPanel = new javax.swing.JPanel();
        footersPanel = new javax.swing.JPanel();
        previousButton = new javax.swing.JButton();
        nextButton = new javax.swing.JButton();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu2 = new javax.swing.JMenu();
        dbConnectionMenuItem = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("NemoVelo");

        middleContentPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Content"));

        footersPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(""));

        previousButton.setText("Previous");
        previousButton.setEnabled(false);
        previousButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                previousButtonActionPerformed(evt);
            }
        });

        nextButton.setText("Next");
        nextButton.setEnabled(false);

        javax.swing.GroupLayout footersPanelLayout = new javax.swing.GroupLayout(footersPanel);
        footersPanel.setLayout(footersPanelLayout);
        footersPanelLayout.setHorizontalGroup(
            footersPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(footersPanelLayout.createSequentialGroup()
                .addGap(61, 61, 61)
                .addComponent(previousButton)
                .addGap(18, 18, 18)
                .addComponent(nextButton)
                .addContainerGap(381, Short.MAX_VALUE))
        );
        footersPanelLayout.setVerticalGroup(
            footersPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(footersPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(footersPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(nextButton)
                    .addComponent(previousButton))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jMenu2.setText("Edit");

        dbConnectionMenuItem.setText("Register");
        dbConnectionMenuItem.setActionCommand("Register Station");
        jMenu2.add(dbConnectionMenuItem);

        jMenuBar1.add(jMenu2);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(middleContentPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 604, Short.MAX_VALUE)
                    .addComponent(footersPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(middleContentPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 366, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(footersPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void previousButtonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_previousButtonActionPerformed
    {//GEN-HEADEREND:event_previousButtonActionPerformed
        previousPanel();
    }//GEN-LAST:event_previousButtonActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new MainWindowFrame().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenuItem dbConnectionMenuItem;
    private javax.swing.JPanel footersPanel;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JPanel middleContentPanel;
    private javax.swing.JButton nextButton;
    private javax.swing.JButton previousButton;
    // End of variables declaration//GEN-END:variables

}
